home *** CD-ROM | disk | FTP | other *** search
/ Aminet 45 / Aminet 45 (2001)(GTI - Schatztruhe)[!][Oct 2001].iso / Aminet / gfx / x11 / x3270_3_2_16.lha / amiga_src / qcpp.c < prev    next >
C/C++ Source or Header  |  2009-01-02  |  4KB  |  189 lines

  1. /*
  2.  * Quick C preprocessor substitute, for converting X3270.ad to X3270.ad.
  3.  *
  4.  * Copyright 1997, 1999, 2000 by Paul Mattes.
  5.  *  Permission to use, copy, modify, and distribute this software and its
  6.  *  documentation for any purpose and without fee is hereby granted,
  7.  *  provided that the above copyright notice appear in all copies and that
  8.  *  both that copyright notice and this permission notice appear in
  9.  *  supporting documentation.
  10.  */
  11.  
  12. /*
  13.  * Understands #if[n]def COLOR, and does the right thing.  All other #ifdefs
  14.  * are assumed to be true.
  15.  */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21.  
  22. #define MAX_NEST    50
  23.  
  24. extern char *optarg;
  25. extern int optind;
  26.  
  27. char *me;
  28. int color = 0;
  29.  
  30. void
  31. usage(void)
  32. {
  33.     fprintf(stderr, "usage: %s [-DCOLOR] [-UCOLOR] [infile [outfile]]\n",
  34.         me);
  35.     exit(1);
  36. }
  37.  
  38. int
  39. main(int argc, char *argv[])
  40. {
  41.     int c;
  42.     char buf[1024];
  43.     FILE *f, *t, *o;
  44.     int nest = 0;
  45.     int ln = 0;
  46.     int pass[MAX_NEST];
  47.     int elsed[MAX_NEST];
  48.  
  49.     if ((me = strrchr(argv[0], '/')) != (char *)NULL)
  50.         me++;
  51.     else
  52.         me = argv[0];
  53.  
  54.     while ((c = getopt(argc, argv, "D:U:")) != -1) {
  55.         switch (c) {
  56.             case 'D':
  57.             if (strcmp(optarg, "COLOR")) {
  58.                 fprintf(stderr, "only -DCOLOR is supported\n");
  59.                 exit(1);
  60.             }
  61.             color = 1;
  62.             break;
  63.             case 'U':
  64.             if (strcmp(optarg, "COLOR")) {
  65.                 fprintf(stderr, "only -UCOLOR is supported\n");
  66.                 exit(1);
  67.             }
  68.             color = 0;
  69.             break;
  70.             default:
  71.             usage();
  72.             break;
  73.         }
  74.     }
  75.     switch (argc - optind) {
  76.         case 0:
  77.         f = stdin;
  78.         break;
  79.         case 1:
  80.         case 2:
  81.         if (strcmp(argv[optind], "-")) {
  82.             f = fopen(argv[optind], "r");
  83.             if (f == (FILE *)NULL) {
  84.                 perror(argv[optind]);
  85.                 exit(1);
  86.             }
  87.         } else
  88.             f = stdin;
  89.         break;
  90.         default:
  91.         usage();
  92.         break;
  93.     }
  94.  
  95.     t = tmpfile();
  96.     if (t == NULL) {
  97.         perror("tmpfile");
  98.         exit(1);
  99.     }
  100.  
  101.     pass[nest] = 1;
  102.  
  103.     while (fgets(buf, sizeof(buf), f) != (char *)NULL) {
  104.         ln++;
  105.         if (buf[0] != '#') {
  106.             if (pass[nest])
  107.                 fprintf(t, "%s", buf);
  108.             continue;
  109.         }
  110.         if (!strcmp(buf, "#ifdef COLOR\n")) {
  111.             pass[nest+1] = pass[nest] && color;
  112.             nest++;
  113.             elsed[nest] = 0;
  114.         } else if (!strncmp(buf, "#ifdef ", 7)) {
  115.             pass[nest+1] = pass[nest];
  116.             nest++;
  117.             elsed[nest] = 0;
  118.         } else if (!strcmp(buf, "#ifndef COLOR\n")) {
  119.             pass[nest+1] = pass[nest] && !color;
  120.             nest++;
  121.             elsed[nest] = 0;
  122.         } else if (!strncmp(buf, "#ifndef ", 8)) {
  123.             pass[nest+1] = 0;
  124.             nest++;
  125.             elsed[nest] = 0;
  126.         } else if (!strcmp(buf, "#else\n")) {
  127.             if (!nest) {
  128.                 fprintf(stderr, "line %d: #else without #if\n",
  129.                     ln);
  130.                 exit(1);
  131.             }
  132.             if (elsed[nest]) {
  133.                 fprintf(stderr, "line %d: duplicate #else\n",
  134.                     ln);
  135.                 exit(1);
  136.             }
  137.             if (pass[nest])
  138.                 pass[nest] = 0;
  139.             else if (pass[nest-1])
  140.                 pass[nest] = 1;
  141.             elsed[nest] = 1;
  142.         } else if (!strcmp(buf, "#endif\n")) {
  143.             if (!nest) {
  144.                 fprintf(stderr, "line %d: #endif without #if\n",
  145.                     ln);
  146.                 exit(1);
  147.             }
  148.             --nest;
  149.         } else {
  150.             fprintf(stderr, "line %d: unknown directive\n", ln);
  151.             exit(1);
  152.         }
  153. #if 0
  154.         fprintf(t, "! line %d nest %d pass[nest] %d\n",
  155.             ln, nest, pass[nest]);
  156. #endif
  157.     }
  158.     if (nest > 0) {
  159.         fprintf(stderr, "missing #endif\n");
  160.         exit(1);
  161.     }
  162.  
  163.     /* Close the input file, if there was one. */
  164.     if (f != stdin)
  165.         fclose(f);
  166.  
  167.     /* Open the output file, if there is one. */
  168.     if (argc - optind == 2) {
  169.         o = fopen(argv[optind + 1], "w");
  170.         if (o == NULL) {
  171.             perror(argv[optind + 1]);
  172.             exit(1);
  173.         }
  174.     } else {
  175.         o = stdout;
  176.     }
  177.  
  178.     /* Copy the temp file to the output file. */
  179.     rewind(t);
  180.     while (fgets(buf, sizeof(buf), t) != NULL) {
  181.         fprintf(o, "%s", buf);
  182.     }
  183.     fclose(t);
  184.     if (o != stdout)
  185.         fclose(o);
  186.  
  187.     return 0;
  188. }
  189.